/**
 * 
 */
package ${basepackage}.manager.impl.op;

import java.util.ArrayList;
import java.util.List;

import com.excalibur.shiro.shiro.util.ShiroUtils;
import com.excalibur.core.util.date.DateUtils;
import com.excalibur.core.util.SpringUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ${basepackage}.constant.SystemConstant;
import ${basepackage}.manager.op.AuthorityManager;
import ${basepackage}.manager.exception.ServiceException;
import ${basepackage}.mapper.op.AuthorityMapper;
import ${basepackage}.model.criteria.op.AuthorityCriteria;
import ${basepackage}.model.po.op.Authority;
import ${basepackage}.model.vo.op.authority.AddAuthorityForm;
import ${basepackage}.model.vo.op.authority.AuthorityRow;
import ${basepackage}.model.vo.op.authority.UpdateAuthorityForm;
import ${basepackage}.model.vo.common.Page;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * @author "Jiance Qin"
 * 
 * @date 2014年2月24日
 * 
 * @time 下午5:33:13
 * 
 */
@Service
public class AuthorityManagerImpl implements AuthorityManager {

	@Autowired
	private AuthorityMapper authorityMapper;

	/**
	 * 
	 */
	public AuthorityManagerImpl() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.general.op.manager.AuthorityManager#delete(java.lang.Long)
	 */
	@Override
	public boolean delete(Long id) throws ServiceException {
		if (id == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.delete.IdNotNull"));
		}
		// 检查权限是否存在
		if (authorityMapper.get(id) == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.delete.NotExist"));
		}
		authorityMapper.remove(id);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.general.op.manager.AuthorityManager#getAuthoritys(com.general.op.
	 * model. criteria.AuthorityCriteria, int, int)
	 */
	@Override
	public Page<AuthorityRow> getAuthoritys(AuthorityCriteria criteria,
			int offset, int limit) throws ServiceException {
		if (criteria == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.getAuthoritys.CriteriaNotNull"));
		}
		if (offset < 0) {
			offset = 0;
		}
		if (limit < 1) {
			limit = 1;
		}

		Page<AuthorityRow> authorityRowList = new Page<AuthorityRow>();
		PageList<Authority> authorityList = authorityMapper.getList(criteria, new PageBounds(offset,
				limit, true));
		authorityRowList.setTotal(Long.valueOf(authorityList.getPaginator().getTotalCount()));

		List<Authority> resultAuthorityList = new ArrayList<Authority>(
				authorityList);
		// 检查当前用户是否为超级管理员，如果不是，则去掉具有超级管理员中心各种权限的角色
		for (Authority autho : authorityList) {
			if (autho.getName().startsWith(SystemConstant.SUPER_ADMIN_AUTHO)) {
				if (!ShiroUtils.isPermitted(SystemConstant.SUPER_ADMIN_AUTHO)) {
					resultAuthorityList.remove(autho);
				}
			}
		}
		List<AuthorityRow> rowList = createAuthorityRowList(resultAuthorityList);
		authorityRowList.setRows(rowList);

		return authorityRowList;
	}

	/**
	 * 创建前台显示权限信息对象列表.
	 * 
	 * @param list
	 * @return
	 */
	protected List<AuthorityRow> createAuthorityRowList(List<Authority> list) {
		if (list == null || list.isEmpty()) {
			return null;
		}
		List<AuthorityRow> rowList = new ArrayList<AuthorityRow>();
		AuthorityRow row;
		for (Authority authority : list) {
			row = createAuthorityRow(authority);
			if (row != null) {
				rowList.add(row);
			}
		}
		return rowList;
	}

	/**
	 * 创建前台显示权限信息对象.
	 * 
	 * @param authority
	 * @return
	 */
	protected AuthorityRow createAuthorityRow(Authority authority) {
		if (authority == null) {
			return null;
		}
		AuthorityRow row = new AuthorityRow();
		row.setId(authority.getId());
		row.setName(authority.getName());
		row.setDisplayName(authority.getDisplayName());
		row.setCreateTime(DateUtils.formatAsString(authority.getCreateTime()));
		row.setUpdateTime(DateUtils.formatAsString(authority.getUpdateTime()));
		return row;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.general.op.manager.AuthorityManager#addAuthority(com.general.op.model
	 * .vo .AddAuthorityForm)
	 */
	@Override
	public AuthorityRow addAuthority(AddAuthorityForm addAuthorityForm)
			throws ServiceException {
		if (addAuthorityForm == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.addAuthority.FormNotNull"));
		}
		// 检查权限名是否存在
		if (isAuthorityNameExist(addAuthorityForm.getName(), null)) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.addAuthority.IsExist"));
		}
		Authority authority = createNewAuthority(addAuthorityForm);
		Long addId = authorityMapper.insert(authority);
		authority = authorityMapper.get(addId);
		return createAuthorityRow(authority);
	}

	/**
	 * 检查权限名是否已存在.
	 * 
	 * @param name
	 * @param oldId
	 * @return
	 */
	protected boolean isAuthorityNameExist(String name, Long oldId) {
		Authority authority = authorityMapper.getByName(name);
		if (authority != null) {
			if (oldId != null) {
				return !oldId.equals(authority.getId());
			}
			return true;
		}
		return false;
	}

	/**
	 * 创建新权限.
	 * 
	 * @param addAuthorityForm
	 * @return
	 */
	protected Authority createNewAuthority(AddAuthorityForm addAuthorityForm) {
		Authority newAuthority = new Authority();

		newAuthority.setName(addAuthorityForm.getName());
		newAuthority.setDisplayName(addAuthorityForm.getDisplayName());

		return newAuthority;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.general.op.manager.AuthorityManager#updateAuthority(com.general.op
	 * .model .vo.UpdateAuthorityForm)
	 */
	@Override
	public AuthorityRow updateAuthority(UpdateAuthorityForm updateAuthorityForm)
			throws ServiceException {
		if (updateAuthorityForm == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.updateAuthority.FormNotNull"));
		}
		if (updateAuthorityForm.getId() == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.updateAuthority.IdNotNull"));
		}
		Authority authority = authorityMapper.get(updateAuthorityForm.getId());
		// 检查权限是否存在
		if (authority == null) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.updateAuthority.NotExist"));
		}
		// 检查权限名是否存在
		if (isAuthorityNameExist(updateAuthorityForm.getName(),
				updateAuthorityForm.getId())) {
			throw new ServiceException(
					SpringUtils
							.getI18nMessage("webapp.service.AuthorityManagerImpl.updateAuthority.IsExist"));
		}
		authority = createUpdateAuthority(updateAuthorityForm);
		Integer ret = authorityMapper.update(authority);
		authority = authorityMapper.get(authority.getId());
		return createAuthorityRow(authority);
	}

	/**
	 * 修改权限.
	 * 
	 * @param updateAuthorityForm
	 * @return
	 */
	protected Authority createUpdateAuthority(
			UpdateAuthorityForm updateAuthorityForm) {
		Authority updateAuthority = new Authority();

		updateAuthority.setId(updateAuthorityForm.getId());
		updateAuthority.setName(updateAuthorityForm.getName());
		updateAuthority.setDisplayName(updateAuthorityForm.getDisplayName());

		return updateAuthority;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.general.op.manager.AuthorityManager#getAll()
	 */
	@Override
	public List<Authority> getAll() throws ServiceException {
		List<Authority> authorityList = authorityMapper.getAll();
		List<Authority> resultAuthorityList = new ArrayList<Authority>(
				authorityList);
		// 检查当前用户是否为超级管理员，如果不是，则去掉具有超级管理员中心各种权限的角色
		for (Authority autho : authorityList) {
			if (autho.getName().startsWith(SystemConstant.SUPER_ADMIN_AUTHO)) {
				if (!ShiroUtils.isPermitted(SystemConstant.SUPER_ADMIN_AUTHO)) {
					resultAuthorityList.remove(autho);
				}
			}
		}
		return (resultAuthorityList == null || resultAuthorityList.isEmpty()) ? null
				: resultAuthorityList;
	}
}
